<!--医院端--订单跟踪--批发采购订单列表-->
<template>
  <div class="channel-template-box">
    <el-container class="template-box">
      <!-- tab搜索 -->

      <!-- 搜索 -->
      <el-header height="auto">
        <tabs
          class="tabs-part"
          :tabs-arr="indexTabsArr"
          @returnIndex="tabsHandle" />
        <SearchForm
          label-width="110px"
          :data="indexSearchData"
          @searchForm="searchForm"
          @resetForm="resetForm"
          :submit-loading="submitLoading"
          :compact-data="compactindexSearchData"
          show-collapse
        />
      </el-header>
      <!-- 列表 -->
      <el-editable
        v-loading="tableLoading"
        :data="dataList"
        height="400"
        :columns="indexTableData">
        <!-- 操作 -->
        <template
          slot="default-action"
          slot-scope="scope">
          <el-button
            v-if="scope.row.orderStatus === 1"
            size="small"
            @click.stop="toDelete(scope.row)"
            type="text text-primary"
          >撤销</el-button>
          <el-button
            v-if="scope.row.orderStatus === 4 || scope.row.orderStatus === 3"
            size="small"
            @click.stop="toReceivingGoods(scope.row)"
            type="text text-primary"
          >收货</el-button>
          <el-button
            size="small"
            @click.stop="toDetails(scope.row)"
            type="text text-primary">详情</el-button>
        </template>
      </el-editable>
      <!-- 分页模块 -->
      <el-pagination
        class="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="curPage"
        :page-sizes="PAGE_NUMBER"
        :page-size="pageSize"
        :layout="PAGINATION_LAYOUT"
        :total="totalElements"
      />
    </el-container>
  </div>
</template>
<script>
import { indexSearchData, indexTableData, indexTabsArr } from './const'
import {
  hospitalWscPurchaseOrderList,
  hospitalwscPurchaseCount,
  hospitalwscOrder
} from '@const/beforeSettleApi'
import SearchForm from '@components/businesscomponents/search'
import ElEditable from '@components/el-editable'
import { PAGE_NUMBER, PAGINATION_LAYOUT } from '@const/common'
import tabs from '@components/tabs/index.vue'
import { cloneDeep, isObject, hasIn } from 'lodash'
export default {
  components: { ElEditable, SearchForm, tabs },
  data () {
    return {
      // 搜索标题字段
      indexSearchData,
      // 搜索返回内容
      searchData: {},
      // 表格loading
      tableLoading: false,
      // 列表数据
      dataList: [],
      // 列表表头
      indexTableData,
      // tab字段
      indexTabsArr,
      submitLoading: false,
      curPage: 1, // 当前页
      PAGE_NUMBER, // 页码数组
      PAGINATION_LAYOUT, // 页码功能布局
      pageSize: PAGE_NUMBER[0], // 一页的数量
      totalElements: 0, // 总共的条数
      tabIndex: 1
    }
  },
  created () {
    this.list()
    this.getNumber()
    // this.indexTabsArr[this.indexTabsArr.length - 1].badge.value = null
    this.indexTabsArr.forEach(item => {
      item.badge.value = null
    })
  },
  computed: {
    compactindexSearchData () {
      return this.indexSearchData.slice(0, 3)
    }
  },
  methods: {
    // 获取列表
    list () {
      // Loading加载开始
      this.submitLoading = true
      this.tableLoading = true
      // 请求列表数据
      this.$axios
        .$post(hospitalWscPurchaseOrderList, {
          ...this.searchData,
          orderStatus: this.tabIndex,
          orderType: 1,
          page: this.curPage,
          pageSize: this.pageSize
        })
        .then(res => {
          let { content, totalElements } = isObject(res.payload)
            ? res.payload
            : {}
          this.dataList = content
          this.totalElements = totalElements
          this.tableLoading = false
        })
        .finally(res => {
          this.tableLoading = false
          this.submitLoading = false
        })
    },
    getNumber () {
      this.$axios({
        method: 'post',
        url: hospitalwscPurchaseCount,
        data: {
          orderType: 1
        }
      }).then(res => {
        let arr = hasIn(res, 'data.payload') ? res.data.payload : []
        if (!arr.length) {
          return
        }
        this.indexTabsArr.forEach(item => {
          arr.forEach(ite => {
            if (ite.statusCode === item.val) {
              item.badge = { value: ite.count }
            }
          })
          if (item.val === 0) {
            item.badge.value = 0
            arr.forEach(ite => {
              item.badge.value += ite.count
            })
          }
        })
      })
    },
    // tabs切换处理事件
    tabsHandle (index) {
      this.tabIndex =
        this.indexTabsArr[index].val === 'null'
          ? null
          : this.indexTabsArr[index].val
      this.curPage = 1
      // 列表
      this.list()
    },

    // 跳转详情
    toDetails (data) {
      this.$router.push({
        path:
          '/hospitalSide/orderTracking/wholesaleOrderList/newDetail',
        query: {
          status: data.orderStatus,
          id: data.purchaseOrderId
        }
      })
    },

    // 撤销
    toDelete (data) {
      this.$axios({
        method: 'post',
        url: hospitalwscOrder,
        data: {
          purchaseOrderId: data.purchaseOrderId,
          orderType: 1
        }
      }).then(res => {
        if (res.data.code === '0') {
          this.list()
          this.indexTabsArr.forEach(item => {
            item.badge.value = null
          })
          this.getNumber()
        } else {
          this.$message({ type: 'warning', message: '撤销失败' })
        }
      })
    },

    // 去收获
    toReceivingGoods (data) {
      this.$router.push({
        path:
          '/hospitalSide/orderTracking/wholesaleOrderList/receiving?id=' +
          data.purchaseOrderId
      })
    },

    // 搜索功能-点击查询
    searchForm (condition) {
      this.searchData = condition
      this.list()
    },

    // 搜索功能-点击重置
    resetForm (condition) {
      this.curPage = 1
      this.searchData = condition
    },
    // 页码更改
    handleSizeChange (size) {
      this.pageSize = size
      this.list()
    },

    // 切换不一样的页面,重置一切数据
    handleCurrentChange (curPage) {
      this.curPage = curPage
      this.list()
    }
  }
}
</script>
<style lang="less">
.channel-template-box {
  .template-box {
    height: 100%;
    overflow: auto;

    .pagination {
      padding-top: 10px;
      display: flex;
      justify-content: flex-end;
      padding-right: 0;
    }
  }
}
</style>
